package mhcs.client;

import java.util.Iterator;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.AbsolutePositionDropController;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.allen_sauer.gwt.dnd.client.drop.GridConstrainedDropController;
import com.google.gwt.event.dom.client.DragHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;

class ManualLandingMap extends ScrollPanel{
	
	private PickupDragController dragController;
	
    public ManualLandingMap() {
    	
    	//SimplePanel test = new SimplePanel();
    	
    	AbsolutePanel ap = new AbsolutePanel();
    	//ap.setPixelSize(1800, 1800);
    	
    	
    	
    	//add(ap);
    	//setWidget(ap);
    	
        //MHCS.dataSet.addListener(this);
        g = new Grid(50, 100);
        g.setCellPadding(0);
        g.setCellSpacing(0);
        //ap.add(g);
        //add(g);
        //add(ap);
        


        // initialize our drag controller
        dragController = new PickupDragController(ap, false);
        //dragController.addDragHandler();
        dragController.setBehaviorMultipleSelection(false);
        

        fillMap();

        
        markUnuseable();
        g.setStyleName("myGrid");
        
        ap.add(g);
        //add(g);
        add(ap);
        
        //ap.add(g);
        //test.add(ap);
        //add(test);
    }

   // @Override
   // public void actionPerformed(String action, Module module) {
//        if (action.equals("clear")) {
//            g.clear();
//        }
//        if (action.equals("remove")) {
//            g.clearCell(50 - module.getY(), module.getX() - 1);
//        }
//        if (action.equals("add")) {
//            Image image = new Image("images/" + module.getType() + ".jpg");
//            image.setPixelSize(20, 20);
//            g.setWidget(50 - module.getY(), module.getX() - 1, image);
//        }
    //}
    
    public void fillMap(){
    	Iterator<Module> manualIter = MainMenu.manualList.iterator();
    	
    	for (int row = 0; row < 50; row++) {
            for (int col = 0; col < 100; col++) {
            	SimplePanel simplePanel = new SimplePanel();
                simplePanel.setPixelSize(20, 20);
                g.setWidget(row, col, simplePanel);
                g.getCellFormatter().setStyleName(row, col, "g");
                
                
                while (manualIter.hasNext()) {
                    Module next = manualIter.next();
                    if (next.getX() == (row+1) && next.getY() == (col+1)){
                    	Image image = new Image("images/" + next.getType() + ".jpg");
                        image.setPixelSize(20, 20);
                        simplePanel.setWidget(image);
                      
                      
                     // make the label draggable
                      dragController.makeDraggable(image);
                    }
                }
                
//             // place a pumpkin in each panel in the cells in the first column
//                if (col == 0) {
//                	Image image = new Image("images/" + "Plain" + ".jpg");
//                    image.setPixelSize(20, 20);
//                    simplePanel.setWidget(image);
//                  
//                  
//                 // make the label draggable
//                  dragController.makeDraggable(image);
//                  
//                }
//                if(MainMenu.manualList)
//                Image image = new Image("images/" + module.getType() + ".jpg");
//                image.setPixelSize(20, 20);
//                simplePanel.setWidget(50 - module.getY(), module.getX() - 1, image);
                
                // make the label draggable
               // dragController.makeDraggable(image);
                
             // instantiate a drop controller of the panel in the current cell
                SetWidgetDropController dropController = new SetWidgetDropController(simplePanel);
                dragController.registerDropController(dropController);
                
            }          
        }
    }
    
    public void markUnuseable() {
        for (int row = 0; row < 11; row++) {
            for (int col = 39; col < 50; col++) {
                Image image = new Image("images/Area.jpg");
                image.setPixelSize(20, 20);
                g.setWidget(row, col, image);
            }
        }
    }
    
    public void clearGrid(){
    	g.clear();  	
    }
    
    private Grid g;
    
}